<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>arttemply中的if else的使用</title>
  <script src="template-web.js"></script>
</head>
<body>
  <table border="1" width="400">
    <thead>
      <tr>
        <th>序号</th>
        <th>ID</th>
        <th>姓名</th>
        <th>年龄</th>
        <th>性别</th>
      </tr>
    </thead>
    <tbody>
      <!-- <tr>
        <td>1</td>
        <td>1</td>
        <td>zs</td>
        <td>32</td>
        <td>男</td>
      </tr> -->
    </tbody>
  </table>
  <script type="text/html" id="tpl">
    {{if data.length != 0}}
      {{each data value index}}
        <tr>
          <td>{{index}}</td>
          <td>{{value.id}}</td>
          <td>{{value.name}}</td>
          <td>{{value.age}}</td>
          <td>{{value.gender}}</td>
        </tr> 
      {{/each}}
    {{else}}
        <tr>
          <td colspan = '5' align = 'center'>没有相关数据</td>
        </tr>
    {{/if}}
  </script>
  <script>
    var obj = {
      code:200,
      data:[
        // {id:'1',name:'张三',age:'20',gender:'男'},
        // {id:'2',name:'李四',age:'21',gender:'男'},
        // {id:'3',name:'王五',age:'23',gender:'女'},
        // {id:'4',name:'赵六',age:'24',gender:'男'},
        // {id:'5',name:'田七',age:'25',gender:'女'},
      ],
    };
    var str = template('tpl',obj);
    document.querySelector('tbody').innerHTML = str;
  </script>
</body>
</html>